# -*- coding: utf-8 -*-

# File Name： line_util
# Description :
# Author : lirui
# create_date： 2022/6/6
# Change Activity:
import math

import numpy as np

from .distance import get_distance


def get_k_b(p1, p2):
    """

    Args:
        p1:
        p2:

    Returns:

    """
    x1, y1 = p1
    x2, y2 = p2
    k = (y1 - y2) / ((x1 - x2) + 10e-9)
    b = (x1 * y2 - x2 * y1) / ((x1 - x2) + 10e-9)
    return k, b


def get_vector_angle(v1, v2):
    """

    Args:
        v2:
        v1: vector1

    Returns:

    """
    if len(v2.shape) == 1:
        v2 = np.expand_dims(v2, axis=0)
    if len(v1.shape) == 1:
        v1 = np.expand_dims(v1, axis=0)
    x1, y1 = v1[:, 0], v1[:, 1]
    x2, y2 = v2[:, 0], v2[:, 1]
    dot = x1 * x2 + y1 * y2
    det = x1 * y2 - y1 * x2
    theta = np.arctan2(det, dot)
    theta = theta * 180 / math.pi
    return theta


def get_poly_degree(poly):
    """

    @param poly:
    @return:
    """
    po = poly.reshape(-1, 2)
    if get_distance(po[1], po[0]) > get_distance(po[2], po[1]):
        v = po[1] - po[0]
    else:
        v = po[2] - po[1]
    v2 = np.array([1, 0])
    theta = abs(get_vector_angle(v, v2))
    return theta
